local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
	
	use "$results_fold/`dataset'.dta", clear

	preserve
		drop if bs == 0 | bs == -1
		drop *_F_* *_N2_* *_N3_*
		collapse (sd) *
		unab varl: *b2* *b3* //*b4*
		foreach var of local varl {
			rename `var' se`var'
		}
		replace bs = 0
		save "$working/temp/bstemp", replace
	restore
	
	preserve
		drop *_F_* *_N2_* *_N3_*
		keep if bs == -1
		unab varl: *b2* *b3* //*b4*
		foreach var of local varl {
			rename `var' se`var'
		}
		expand 3
		save "$working/temp/bstemp2", replace
	restore
	
	keep if bs == 0
	merge 1:1 bs using "$working/temp/bstemp"
	drop _merge 
	keep  *b2* *b3* *N2_* *N3_* // *b4*
	expand 13
	append using "$working/temp/bstemp2"
	gen id = _n
	replace id = 1.5 if id == 14
	replace id = 3.5 if id == 15
	replace id = 5.5 if id == 16
	drop bs
	sort id
	unab varlist: *b2* *b3* // *b4*
	local noinst: subinstr local varlist "I_" "", all
	local mainvars: subinstr local noinst "se" "", all
	local mainvars: subinstr local mainvars "F_" "", all
	local mainvars_uniq : list uniq mainvars
	foreach v of varlist `mainvars_uniq' {
		replace `v' = . if id > 1 
		local temp "se`v'"
		replace `temp' = . if id != 2 & id != 1.5
	}

	unab varlist2: *I_b2*
	local I2vars: subinstr local varlist2 "se" "", all
	local I2vars: subinstr local I2vars "F_" "", all
	local I2vars_uniq : list uniq I2vars
	foreach v of varlist `I2vars_uniq' {
		replace `v' = . if id != 3
		local temp "se`v'"
		replace `temp' = . if id != 4 & id != 3.5
	}
	
	unab varlist3: *I_b3*
	local I3vars: subinstr local varlist3 "se" "", all
	local I3vars: subinstr local I3vars "F_" "", all
	local I3vars_uniq : list uniq I3vars
	foreach v of varlist `I3vars_uniq' {
		replace `v' = . if id != 5
		local temp "se`v'"
		replace `temp' = . if id != 6 & id != 5.5
	}

	unab vars: se*
	local newvars: subinstr local vars "se" "", all
	foreach var of varlist `newvars' {
		local temp "se`var'"
		*replace `temp' = `temp'[_n-1] if missing(`temp') 
		tostring `temp', replace format(%9.3f) force
		replace `temp' = "[" + `temp' + "]" if id == 2 | id == 4 | id == 6
		replace `temp' = "(" + `temp' + ")" if id == 1.5 | id == 3.5 | id == 5.5
		tostring `var', replace format(%9.3f) force
		replace `var' = `temp' if `var' == "."
		drop `temp'
		replace `var' = "." if `var' == "(.)" | `var' == "[.]"
	}

	unab varlistf: *_F_I*
	foreach v of varlist `varlistf' {
		replace `v' = . if id != 9
		tostring `v', replace format(%9.1f) force
		replace `v' = "" if `v' == "."
		local temp: subinstr local v "F_I_" "", all
		replace `temp' = `v' if `temp' == "." | `temp' == ""
		drop `v'
	}
	unab varlistf: *_F_*
	foreach v of varlist `varlistf' {
		replace `v' = . if id != 8
		tostring `v', replace format(%9.1f) force
		replace `v' = "" if `v' == "."
		local temp: subinstr local v "F_" "", all
		replace `temp' = `v' if `temp' == "." | `temp' == ""
		drop `v'
	}
	forval i = 2/3 {
		unab varlistn: *N`i'* 
		foreach v of varlist `varlistn' {
			replace `v' = . if id != 10
			tostring `v', replace format(%9.0f) force
			replace `v' = "" if `v' == "."
			local temp: subinstr local v "N`i'" "b`i'", all
			replace `temp' = `v' if `temp' == "." | `temp' == ""
			drop `v'
		}
	}

	unab varlist2: *I_b2*
	foreach v of varlist `varlist2' {
		local temp: subinstr local v "I_" "", all
		replace `temp' = `v' if `temp' == ""
		drop `v'
	}
	
	unab varlist3: *I_b3*
		foreach v of varlist `varlist3' {
		local temp: subinstr local v "I_" "", all
		replace `temp' = `v' if `temp' == ""
		drop `v'
	}

	unab varlist: *b*
	foreach v of varlist `varlist' {
		replace `v' = "" if `v' == "."
	}
		


	gen v = "OutputComp" if id == 1
	replace v = "InputAccess" if id == 3
	replace v = "InputAccessIV" if id == 5
	replace v = "F-Stat OutputComp" if id == 8
	replace v = "F-Stat InputAccess" if id == 9
	replace v = "Observations" if id == 10
	replace v = "Year FE" if id == 11
	replace v = "Industry FE" if id == 12
	replace v = "Industry-Year FE" if id == 13
	
	

	unab varlist: *_y
	foreach v of varlist `varlist' {
		replace `v' = "Yes" if id == 11
		replace `v' = "No" if id == 12 | id == 13
	}

	unab varlist: *_yn
	foreach v of varlist `varlist' {
		replace `v' = "Yes" if id == 11 | id == 12
		replace `v' = "No" if  id == 13
	}

	unab varlist: *_ycn
	foreach v of varlist `varlist' {
		replace `v' = "Yes" if id == 13
		replace `v' = "No" if id == 11 | id == 12
	}
	
	texsave v diff_tfpr_b2_y diff_tfpr_b2_yn diff_tfpr_b2_ycn diff_tfpr_b3_y diff_tfpr_b3_yn diff_tfpr_b3_ycn using "$tables/firmtable_`samplenamefile'.tex", ///
		footnote("Notes: Reports results from IV estimation of (\ref{eq: causal-specification}) where the $O_{it}=\Delta \ln(Z_{it})$. See text for further details. Analytical SE clustered by 4-digit industry in (.) , Bootstrapped SEs clustered by firm in [.]") ///
		title("Firm-level Revenue TFP Regressions") ///
		nofix replace location(h) varlab marker(tab:FTFPR) frag ///
		autonumber width(1.1\textwidth)
	unab varlist: diff_tfpr_b*
